<template>
  <div
    class="app-container flex flex-direction flex-sub"
    style="background: none; height: 100%; padding: 1rem"
  >
    <el-card shadow="never" class="box-card flex-sub" style="overflow: auto">
      <div class="div-query">
        <div class="collapse">
          <div class="collapse-header" @click="panelActive = !panelActive">
            <i
              class="collapse-header-icon"
              :class="[
                panelActive ? 'el-icon-caret-bottom' : 'el-icon-caret-top',
              ]"
            />
            {{ panelActive ? '隐藏' : '展开' }}查询条件
          </div>
          <div class="collapse-content">
            <el-collapse-transition>
              <div v-show="panelActive">
                <el-form size="small" label-position="right">
                  <el-row>
                    <el-col :span="8">
                      <el-form-item label="用户名称" label-width="80px">
                        <el-input
                          v-model="queryParams.username"
                          :placeholder="$t('table.user.username')"
                          clearable
                          class="filter-item search-item"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="部门名称" label-width="80px">
                        <el-input
                          v-model="queryParams.deptName"
                          :placeholder="$t('table.user.dept')"
                          clearable
                          class="filter-item search-item"
                        />
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="8">
                      <el-form-item label="创建时间" label-width="80px">
                        <el-date-picker
                          v-model="queryParams.timeRange"
                          type="datetimerange"
                          range-separator="至"
                          start-placeholder="创建开始时间"
                          end-placeholder="创建结束时间"
                          align="right"
                        />
                      </el-form-item>
                    </el-col>

                    <el-col :span="8">
                      <el-form-item style="margin-left: 10px">
                        <el-button
                          class="filter-item"
                          type="primary"
                          @click="search"
                          style="margin-right: 10px"
                        >
                          {{ $t('table.search') }}
                        </el-button>
                        <el-button
                          class="filter-item"
                          type="info"
                          @click="reset"
                          style="margin-right: 10px"
                        >
                          {{ $t('table.reset') }}
                        </el-button>

                        <el-dropdown
                          v-has-any-permission="[
                            'user:add',
                            'user:delete',
                            'user:reset',
                            'user:export',
                          ]"
                          trigger="click"
                          class="filter-item"
                        >
                          <el-button>
                            {{ $t('table.more')
                            }}<i class="el-icon-arrow-down el-icon--right" />
                          </el-button>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              v-has-permission="['user:add']"
                              @click.native="add"
                              >{{ $t('table.add') }}</el-dropdown-item
                            >
                            <el-dropdown-item
                              v-has-permission="['user:delete']"
                              @click.native="batchDelete"
                              >{{ $t('table.delete') }}</el-dropdown-item
                            >
                            <el-dropdown-item
                              v-has-permission="['user:reset']"
                              @click.native="resetPassword"
                              >{{ $t('table.resetPassword') }}</el-dropdown-item
                            >
                            <el-dropdown-item
                              v-has-permission="['user:export']"
                              @click.native="exportExcel"
                              >{{ $t('table.export') }}</el-dropdown-item
                            >
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </el-collapse-transition>
          </div>
        </div>
      </div>

      <!-- <div class="filter-container flex" style="align-content: center">
        <div>
          <el-input
            v-model="queryParams.username"
            :placeholder="$t('table.user.username')"
            class="filter-item search-item"
          />
        </div>
        <div>
          <el-input
            v-model="queryParams.deptName"
            :placeholder="$t('table.user.dept')"
            class="filter-item search-item"
          />
        </div>
        <div>
          <el-date-picker
            v-model="queryParams.timeRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="创建开始时间"
            end-placeholder="创建结束时间"
            align="right"
          />
        </div>
        <div style="margin-left: 0.7rem">
          <el-button class="filter-item" type="primary" @click="search">
            {{ $t('table.search') }}
          </el-button>
        </div>
        <div style="margin-left: 0.7rem">
          <el-button class="filter-item" type="info" @click="reset">
            {{ $t('table.reset') }}
          </el-button>
        </div>
        <div style="margin-left: 0.7rem">
          <el-dropdown
            v-has-any-permission="[
              'user:add',
              'user:delete',
              'user:reset',
              'user:export',
            ]"
            trigger="click"
            class="filter-item"
          >
            <el-button>
              {{ $t('table.more')
              }}<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-has-permission="['user:add']"
                @click.native="add"
                >{{ $t('table.add') }}</el-dropdown-item
              >
              <el-dropdown-item
                v-has-permission="['user:delete']"
                @click.native="batchDelete"
                >{{ $t('table.delete') }}</el-dropdown-item
              >
              <el-dropdown-item
                v-has-permission="['user:reset']"
                @click.native="resetPassword"
                >{{ $t('table.resetPassword') }}</el-dropdown-item
              >
              <el-dropdown-item
                v-has-permission="['user:export']"
                @click.native="exportExcel"
                >{{ $t('table.export') }}</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div> -->

      <el-table
        ref="table"
        :key="tableKey"
        size="medium"
        v-loading="loading"
        :data="list"
        border
        fit
        style="width: 100%"
        :header-cell-style="{ background: '#fafafa' }"
        @selection-change="onSelectChange"
        @sort-change="sortChange"
      >
        <el-table-column
          type="selection"
          align="center"
          width="40px"
          fixed
          :reserve-selection="true"
        />
        <el-table-column
          :label="$t('table.user.username')"
          prop="username"
          :show-overflow-tooltip="true"
          align="center"
          min-width="120px"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('table.user.sex')"
          :filters="[
            { text: $t('common.sex.male'), value: '0' },
            { text: $t('common.sex.female'), value: '1' },
            { text: $t('common.sex.secret'), value: '2' },
          ]"
          :filter-method="filterSex"
          class-name="status-col"
        >
          <template slot-scope="{ row }">
            <el-tag size="medium" :type="row.sex | sexFilter">
              {{ transSex(row.sex) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('table.user.email')"
          :show-overflow-tooltip="true"
          align="center"
          min-width="150px"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.email }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('table.user.dept')"
          align="center"
          min-width="100px"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.deptName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('table.user.status')"
          :filters="[
            { text: $t('common.status.valid'), value: '1' },
            { text: $t('common.status.invalid'), value: '0' },
          ]"
          :filter-method="filterStatus"
          class-name="status-col"
        >
          <template slot-scope="{ row }">
            <el-tag size="medium" :type="row.status | statusFilter">
              {{
                row.status === '1'
                  ? $t('common.status.valid')
                  : $t('common.status.invalid')
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('table.user.createTime')"
          prop="createTime"
          align="center"
          min-width="180px"
          sortable="custom"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('table.operation')"
          fixed="right"
          width="120px"
          class-name="small-padding fixed-width"
          align="center"
        >
          <template slot-scope="{ row }">
            <el-tooltip effect="dark" content="详情" placement="top-start">
              <i
                class="iconfont icon-chakanxiangqing2 i-detail"
                @click="view(row)"
                v-hasPermission="['user:view']"
              />
            </el-tooltip>

            <i class="iconfont icon-fengefu i-fengefu" />

            <el-tooltip effect="dark" content="编辑" placement="top-start">
              <i
                class="iconfont icon-bianji2 i-edit"
                @click="edit(row, 'edit')"
                v-hasPermission="['user:update']"
              />
            </el-tooltip>
            <i class="iconfont icon-fengefu i-fengefu" />
            <el-tooltip effect="dark" content="删除" placement="top-start">
              <i
                class="iconfont icon-shanchu i-delete"
                @click="singleDelete(row)"
                v-hasPermission="['user:delete']"
              />
            </el-tooltip>
            <el-link
              v-has-no-permission="['user:view', 'user:update', 'user:delete']"
              class="no-perm"
            >
              {{ $t('tips.noPermission') }}
            </el-link>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="pagination.num"
        :limit.sync="pagination.size"
        @pagination="search"
      />
    </el-card>
    <user-edit
      ref="edit"
      :dialog-visible="dialog.isVisible"
      :title="dialog.title"
      @success="editSuccess"
      @close="editClose"
    />
    <user-view
      ref="view"
      :dialog-visible="userViewVisible"
      @close="viewClose"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import UserEdit from './Edit'
import UserView from './View'
import dataTableMixins from '@/mixins/data-table-mixins'
export default {
  name: 'UserManage',
  components: { Pagination, UserEdit, UserView },
  mixins: [dataTableMixins],
  filters: {
    sexFilter(status) {
      const map = {
        0: 'success',
        1: 'danger',
        2: 'info',
      }
      return map[status]
    },
    statusFilter(status) {
      const map = {
        0: 'danger',
        1: 'success',
      }
      return map[status]
    },
  },
  data() {
    return {
      dialog: {
        isVisible: false,
        title: '',
      },
      userViewVisible: false,
      tableKey: 0,
      loading: false,
      list: null,
      total: 0,
      queryParams: {},
      sort: {},
      selection: [],
      pagination: {
        size: 10,
        num: 1,
      },
    }
  },
  computed: {
    currentUser() {
      return this.$store.state.account.user
    },
  },
  mounted() {
    this.fetch()
  },
  methods: {
    transSex(sex) {
      switch (sex) {
        case '0':
          return this.$t('common.sex.male')
        case '1':
          return this.$t('common.sex.female')
        default:
          return this.$t('common.sex.secret')
      }
    },
    filterStatus(value, row) {
      return row.status === value
    },
    filterSex(value, row) {
      return row.sex === value
    },
    viewClose() {
      this.userViewVisible = false
    },
    editClose() {
      this.dialog.isVisible = false
    },
    editSuccess() {
      this.search()
    },
    onSelectChange(selection) {
      this.selection = selection
    },
    search() {
      this.fetch({
        ...this.queryParams,
        ...this.sort,
      })
    },
    reset() {
      this.queryParams = {}
      this.sort = {}
      this.$refs.table.clearSort()
      this.$refs.table.clearFilter()
      this.search()
    },
    exportExcel() {
      this.$download(
        'system/user/excel',
        {
          pageSize: this.pagination.size,
          pageNum: this.pagination.num,
          ...this.queryParams,
        },
        `user_${new Date().getTime()}.xlsx`
      )
    },
    add() {
      this.dialog.title = '用户管理 ＞新增'
      this.dialog.isVisible = true
    },
    resetPassword() {
      //debug
      if (!this.selection.length) {
        this.$message({
          message: this.$t('tips.noDataSelected'),
          type: 'warning',
        })
        return
      }
      this.$confirm(
        this.$t('tips.confirmRestPassword'),
        this.$t('common.tips'),
        {
          confirmButtonText: this.$t('common.confirm'),
          cancelButtonText: this.$t('common.cancel'),
          type: 'warning',
        }
      )
        .then(() => {
          const userNames = []
          this.selection.forEach((u) => {
            userNames.push(u.username)
          })
          this.$put('system/user/password/reset', {
            usernames: userNames.join(','),
          }).then(() => {
            this.$message({
              message: this.$t('tips.resetPasswordSuccess'),
              type: 'success',
            })
            this.clearSelections()
          })
        })
        .catch(() => {
          this.clearSelections()
        })
    },
    singleDelete(row) {
      this.$refs.table.toggleRowSelection(row, true)
      this.batchDelete()
    },
    batchDelete() {
      if (!this.selection.length) {
        this.$message({
          message: this.$t('tips.noDataSelected'),
          type: 'warning',
        })
        return
      }
      let contain = false
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning',
      })
        .then(() => {
          const userIds = []
          this.selection.forEach((u) => {
            if (u.userId === this.currentUser.userId) {
              contain = true
              return
            }
            userIds.push(u.userId)
          })
          if (contain) {
            this.$message({
              message: this.$t('tips.containCurrentUser'),
              type: 'warning',
            })
            this.clearSelections()
          } else {
            this.delete(userIds)
          }
        })
        .catch(() => {
          this.clearSelections()
        })
    },
    clearSelections() {
      this.$refs.table.clearSelection()
    },
    delete(userIds) {
      this.loading = true
      this.$delete(`system/user/${userIds}`).then(() => {
        this.$message({
          message: this.$t('tips.deleteSuccess'),
          type: 'success',
        })
        this.search()
      })
    },
    view(row) {
      this.$refs.view.setUser(row)
      this.userViewVisible = true
    },
    edit(row) {
      let roleId = []
      if (row.roleId && typeof row.roleId === 'string') {
        roleId = row.roleId.split(',')
        row.roleId = roleId
      }
      this.$get(`system/user/${row.userId}`).then((r) => {
        row.deptIds = r.data.data
        this.$refs.edit.setUser(row)
        this.dialog.title = '用户管理 ＞编辑'
        this.dialog.isVisible = true
      })
    },
    fetch(params = {}) {
      params.pageSize = this.pagination.size
      params.pageNum = this.pagination.num
      if (this.queryParams.timeRange) {
        params.createTimeFrom = this.$moment(
          this.queryParams.timeRange[0]
        ).format('YYYY-MM-DD HH:mm:ss')
        params.createTimeTo = this.$moment(
          this.queryParams.timeRange[1]
        ).format('YYYY-MM-DD HH:mm:ss')
      }
      this.loading = true
      this.$get('system/user', {
        ...params,
      }).then((r) => {
        const data = r.data.data
        this.total = data.total
        this.list = data.rows
        this.loading = false
      })
    },
    sortChange(val) {
      this.sort.field = val.prop
      this.sort.order = val.order
      this.search()
    },
  },
}
</script>
<style lang="scss" scoped>
@import '../../../../styles/part-custom';
</style>
